package com.zijing2333.adaptivescheduling.SwitcherManager.utils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public final class NetworkUtil {

    private NetworkUtil() {

    }


    public static String getIP(Context context) {  //获取IP地址
        //这里的WIFI_SERVICE对应之前在AndroidManifest.xml中配置的WIFI状态读取权限
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifiManager.getConnectionInfo();
        int ipAddress = info.getIpAddress();
        return intToIp(ipAddress);
    }

//    获取的ip为一个十进制的数值。但是这个十进制数通常不是我们想要的，要将这个十进制数转化为常见的ip地址
    private static String intToIp(int ipInt) {
        return (ipInt & 0xFF) + "." +
                ((ipInt >> 8) & 0xFF) + "." +
                ((ipInt >> 16) & 0xFF) + "." +
                ((ipInt >> 24) & 0xFF);
    }
}
